package jp.co.ikd.util;

import static jp.co.ikd.util.Util.asHex;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hash {

	private static final String HASH_ALGORITHM = "SHA-256";

	public static String getHexDigest(String str) {

		if (str == null) {
			return null;
		}
		return getHexDigest(str.getBytes());
	}

	public static String getHexDigest(byte[] b) {

		if (b.length <= 0) {
			return null;
		}

		MessageDigest md = null;

		try {
			md = MessageDigest.getInstance(HASH_ALGORITHM);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
		md.update(b);

		byte[] toChapter1Digest = md.digest();

		return asHex(toChapter1Digest);
	}
}